﻿Imports System.IO
Imports OpenTK.Graphics.OpenGL
Public Class ModFile
    Public VertStart As UInteger = 0
    Public VertEnd As UInteger = 0
    Public VertCnt As UInteger = 0
    Public PolyCnt As UInteger = 0
    Public PolyEnd As UInteger = 0
    Public ModelCnt As UInteger = 0
    Public unk As UInteger = 0
    Public Buffer(-1) As Byte
    Public Verts() As Vertex
    Public Polygons() As N3DFace
    Structure ModelGroup
        Dim stVert As Integer
        Dim endVert As Integer
    End Structure
    Sub New(ByVal fn As String)
        If File.Exists(fn) Then
            Dim fs As New FileStream(fn, FileMode.Open)
            ReDim Buffer(fs.Length - 1)
            fs.Read(Buffer, 0, fs.Length - 1)
            fs.Dispose()
            Parse()
        End If
    End Sub
    Private Sub Parse()

        If Buffer(0) = &H4D And Buffer(1) = &H4F And Buffer(2) = &H44 Then
            VertStart = BitConverter.ToUInt32(Buffer, &H34)
            VertEnd = BitConverter.ToUInt32(Buffer, &H38)
            PolyEnd = BitConverter.ToUInt32(Buffer, &H3C)
            VertCnt = BitConverter.ToUInt32(Buffer, &HC)
            PolyCnt = BitConverter.ToUInt32(Buffer, &H14)
            ModelCnt = BitConverter.ToUInt16(Buffer, &H4)

            Dim Pos As UInteger = VertStart
            ReDim Verts(VertCnt - 1)
            ReDim Polygons(PolyCnt - 1)


            For i As Integer = 0 To VertCnt - 1

                With Verts(i)

                    .x = BitConverter.ToSingle(Buffer, Pos)
                    .y = BitConverter.ToSingle(Buffer, Pos + 4)
                    .z = BitConverter.ToSingle(Buffer, Pos + 8)

                    Pos += &H14

                End With
            Next
            Pos = VertEnd
            Dim j As Integer = 0
            For j = 0 To PolyCnt - 1
                With Polygons(j)
                    ReDim .v(2)
                    .v(0) = Buffer(Pos + 2)
                    .v(1) = Buffer(Pos + 1)
                    .v(2) = Buffer(Pos)

                End With
                Pos += 3
            Next
            MsgBox(Hex(Pos))
        End If
    End Sub
    Public Sub Draw(Optional ByVal rMode As BeginMode = BeginMode.Triangles)
        If VertCnt > 0 And PolyCnt > 0 Then

            GL.PointSize(5)
            GL.Color3(Color.Red)
            GL.Begin(rMode)
            For i As Integer = 0 To VertCnt - 1

                'pA = m.Polygons(i).v(0)
                'GL.Vertex3(m.Verts(pA).x, m.Verts(pA).y, m.Verts(pA).z)

                'pB = m.Polygons(i).v(1)
                'GL.Vertex3(m.Verts(pB).x, m.Verts(pB).y, m.Verts(pB).z)

                'pC = m.Polygons(i).v(2)
                'GL.Vertex3(m.Verts(pC).x, m.Verts(pC).y, m.Verts(pC).z)

                GL.Vertex3(Verts(i).x, Verts(i).y, Verts(i).z)
            Next
            GL.End()
        End If
    End Sub
End Class
